package com.glory.dgcpcpda.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.BinInfoEntity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.EquipMentEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.PackageDetailListEntity;
import com.glory.dgcpcpda.entity.PackageEntity;
import com.glory.dgcpcpda.entity.RecipeInfoEntity;
import com.glory.dgcpcpda.entity.ScrapEntity;
import com.glory.dgcpcpda.entity.StrzycxEntity;
import com.glory.dgcpcpda.entity.ToolEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import org.json.JSONObject;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * FileName: LotUtils
 * Author: XJH
 * Date: 2020/5/26 16:38
 * Description:常规操作
 * History:
 */
public class LotUtils {

    public interface netCallback<T>{
        void onSuccess(T response);
        void onFail(Throwable t);
    }

    public interface TrackOutCallback<T>{
        void onFail(Throwable t);
        void onCancel();
    }

    public static void searchLotInfo(Context context,String lotID, FragmentManager fragmentManager, netCallback listener) {
        FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                ("正在查询LOT信息",new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,true,fragmentManager)){
                    if (listener != null){
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }else {
                    if (listener != null){
                        listener.onFail(response.getException());
                    }
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onError(response);
                if (listener != null){
                    listener.onFail(response.getException());
                }
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","LOTINFOBYID");
        map.addProperty("LOTID",lotID);
        map.addProperty("LOTALIAS","");
        map.addProperty("DURABLE","");
        map.addProperty("EQUIPMENTID","");
        NetUtils.requestPostNet("CPC.LOTQUERY","LOTQUERY",context,map,callBack);
//        NetUtils.requestMESPostNet("CPC.LOTQUERY",context,map,callBack);
    }

    public static void checkCarrier(Context context,String lotID,String carrierID,FragmentManager fragmentManager, netCallback listener) {
        FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                ("正在验证载具信息",new TypeToken<BaseResponseBean<Object>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<Object>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,false,fragmentManager)){
                    if (listener != null){
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<Object>> response) {
                super.onError(response);
                if (listener != null){
                    listener.onFail(response.getException());
                }
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","BIND");
        map.addProperty("LOTID",lotID);
        map.addProperty("CARRIERID",carrierID);
        NetUtils.requestPostNet("CPC.CHECKCARRIER","TRANSFERMES",context,map,callBack);
    }

    // GD1775 获取程序名查询批次接口
    public static void searchLotInfoStr(Context context,String lotID, FragmentManager fragmentManager, netCallback listener) {
        FreeUI_EntityCallBack<BaseResponseBean<StrzycxEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<StrzycxEntity>>
                ("正在查询LOT信息",new TypeToken<BaseResponseBean<StrzycxEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<StrzycxEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,true,fragmentManager)){
                    if (listener != null){
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }else {
                    if (listener != null){
                        listener.onFail(response.getException());
                    }
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<StrzycxEntity>> response) {
                super.onError(response);
                if (listener != null){
                    listener.onFail(response.getException());
                }
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotID);
        NetUtils.requestPostCPC("CPC.LOTQUERYSTR","GetSTRproGram",context,map,callBack);
    }

    public static void searchEqpInfo(String eqpID,Context context,FragmentManager fragmentManager,netCallback listener){
        FreeUI_EntityCallBack<BaseResponseBean<EqpInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EqpInfoEntity>>
                ("正在查询设备信息",new TypeToken<BaseResponseBean<EqpInfoEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,true,fragmentManager)){
                    if (listener != null){
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpInfoEntity>> response) {
                super.onError(response);
                if (listener != null){
                    listener.onFail(response.getException());
                }
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","LOAD");
        EquipMentEntity equipMentEntity = new EquipMentEntity();
        equipMentEntity.setEQUIPMENTID(eqpID);
        map.add("EQUIPMENT",new JsonParser().parse(new Gson().toJson(equipMentEntity)).getAsJsonObject());
        NetUtils.requestPostNet("CPC.EQPQUERY","EQPQUERY",context,map,callBack);
    }

    public static void lotTrackIn(FragmentManager fragmentManager, Activity context,String user,String eqpID,String lotID,List<String> carrierList,String actionType){
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                //确定

                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context,false,fragmentManager)){
                            FreeApi_ToastUtils.showFreeToast("进站成功",context,true,0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKIN");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE",actionType);
                map.addProperty("EQUIPMENTID",eqpID);
                map.addProperty("ISCHECKSTEPAUTHORITY",true);
                map.add("DURABLELIST",new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(lotID));
                map.add("LOTLIST",new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
//                map.addProperty("LOTACTIONLIST",etLot.getText().toString());//传批次动作列表
                NetUtils.mRequestPostNet("CPC.LOTTRACKIN",user,"LOTTRACKIN",context,map,callBack);

            }
        }, () -> {
        }).show(fragmentManager,"SUBMIT_DIALOG");
    }

    public static void lotBindTool(Activity context,FragmentManager fragmentManager,String toolID,String eqpID,boolean actionType){
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context,false,fragmentManager)){
                            FreeApi_ToastUtils.showFreeToast(actionType ? "绑定成功" : "解绑成功",context,true,0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "TOOLREPORT");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE",actionType ? "UP" : "DOWN");
                map.addProperty("EQUIPMENTID",eqpID);
                List<ToolEntity> toolList = new ArrayList<>();
                ToolEntity toolEntity = new ToolEntity(toolID);
                toolList.add(toolEntity);
                map.add("TOOLLIST",new JsonParser().parse(new Gson().toJson(toolList)).getAsJsonArray());
                NetUtils.requestPostNet("CPC.TOOLREPORT","TOOLREPORT",context,map,callBack);
            }
        }, () -> {

        }).show(fragmentManager,"TOOLREPORT");
    }

    public static void searchRecipeInfo(Context context,FragmentManager fragmentManager,LotInfoEntity lotInfoEntity,String ChipName,String eqpInfoEntity,netCallback listener,String processName,String eqpType,String plantName,String resin,String Customercode){
        FreeUI_EntityCallBack<BaseResponseBean<RecipeInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<RecipeInfoEntity>>
                ("自定义loading",new TypeToken<BaseResponseBean<RecipeInfoEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<RecipeInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,false,fragmentManager)){
                    if (listener != null){
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<RecipeInfoEntity>> response) {
                super.onError(response);
                if (listener != null){
                    listener.onFail(response.getException());
                }
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ProcessName",processName);
        map.addProperty("PkgType",lotInfoEntity.getLOT().getMAINMATTYPE());
        map.addProperty("BDID",lotInfoEntity.getWIRINGNO());
        int res=lotInfoEntity.getWIRESPEC().indexOf("/",lotInfoEntity.getWIRESPEC().indexOf("/")+1);
        if(res>0){
            map.addProperty("WireType",lotInfoEntity.getWIRESPEC().substring(0,res).toUpperCase());
        }
        else{
            map.addProperty("WireType",lotInfoEntity.getWIRESPEC().toUpperCase());
        }
        //map.addProperty("WireType",lotInfoEntity.getWIRESPEC());
        map.addProperty("DeviceName",lotInfoEntity.getPRODUCTMODEL());
        map.addProperty("ChipName",ChipName);
        map.addProperty("LineID",lotInfoEntity.getLINEID());
        map.addProperty("CUSTOMERPARTID",lotInfoEntity.getLOT().getCUSTOMERPARTID());
        map.addProperty("TPNID",lotInfoEntity.getTPNID());
        map.addProperty("EqpID",eqpInfoEntity);
        map.addProperty("LotID", lotInfoEntity.getLOT().getLOTID());
        map.addProperty("EqpType",eqpType);
        map.addProperty("PlantName",plantName);
        map.addProperty("Resin",resin);
        map.addProperty("Customercode",Customercode);
        map.addProperty("Wafersize",lotInfoEntity.getWAFERSIZE());
        map.addProperty("Jmthickness",lotInfoEntity.getJMTHICKNESS());
        map.addProperty("ISPO",lotInfoEntity.getISPO());
        map.addProperty("Materialtype",lotInfoEntity.getYANMO());
        map.addProperty("ModelNo",lotInfoEntity.getHUAPIAN());
        NetUtils.requestPostNet("CPC.GetRecipeAvailable","GetRecipeAvailable",context,map,callBack);
    }

    public static void recipeDownLoad(Activity context,FragmentManager fragmentManager,RecipeInfoEntity recipeInfoEntity,String eqpID,String lotID,int enableEQ){
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,false,fragmentManager)){
                    FreeApi_ToastUtils.showFreeToast("下载成功",context,true,0);
                    context.finish();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "SendEqpRecipe");
                FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("PlantName",recipeInfoEntity.getPlantName());
        map.addProperty("ProcessName",recipeInfoEntity.getProcessName());
        map.addProperty("EqpID",eqpID);
        map.addProperty("RecipeEqpID",recipeInfoEntity.getRecipeEqpID());
        map.addProperty("RecipeID",recipeInfoEntity.getRecipeID());
        map.addProperty("RecipeVer",recipeInfoEntity.getRecipeVer());
        map.addProperty("LotID",lotID);
        map.addProperty("EnableEQ",enableEQ);
        NetUtils.requestPostNet("CPC.SendEqpRecipe","SendEqpRecipe",context,map,callBack);
    }

    public static void publicAudit(Context context,FragmentManager fragmentManager,String lotID,String packLotID,String packageType,netCallback listener){
        FreeUI_EntityCallBack<BaseResponseBean<PackageDetailListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageDetailListEntity>>
                (new TypeToken<BaseResponseBean<PackageDetailListEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context,false,fragmentManager)){
                    if (listener != null){
                        PackageDetailListEntity body = response.body().getResponse().getBody();
                        body.setPACKLOTID(packLotID);
                        body.setSOURCELOTID(lotID);
                        listener.onSuccess(body);
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                super.onError(response);
                if (listener != null){
                    listener.onFail(response.getException());
                }

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","CHECK");
        map.addProperty("LOTID",lotID);
        map.addProperty("PACKLOTID",packLotID);
        map.addProperty("PACKAGETYPE",packageType);
        NetUtils.requestPostNet("CPC.PUBLICAUDIT","PUBLICAUDIT",context,map,callBack);

    }

    public static void publicAudit(Activity context,FragmentManager fragmentManager,String actionType,String packagrType,List<PackageDetailListEntity> data){
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                FreeUI_EntityCallBack<BaseResponseBean<PackageDetailListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageDetailListEntity>>
                        (new TypeToken<BaseResponseBean<PackageDetailListEntity>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context,false,fragmentManager)){
                            FreeApi_ToastUtils.showFreeToast("核对成功",context,true,0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "PUBLICAUDIT");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);

                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE",actionType);
                map.addProperty("PACKAGETYPE",packagrType);
                map.add("PACKAGEDETAILLIST",new JsonParser().parse(new Gson().toJson(data)).getAsJsonArray());
                NetUtils.requestPostNet("CPC.PUBLICAUDIT","PUBLICAUDIT",context,map,callBack);
            }
        }, () -> {

        }).show(fragmentManager,"TOOLATTACHEQP");

    }


    public static void packageUpShelf(Activity context,FragmentManager fragmentManager,String lotID,String storageID,boolean packageUp,String consumeQTY,String isEndFlag,netCallback listener){
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageEntity>>
                        (new TypeToken<BaseResponseBean<PackageEntity>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context,false,fragmentManager)){
                            if (listener != null){
                                listener.onSuccess(response.body().getResponse().getBody());
                                return;
                            }
                            FreeApi_ToastUtils.showFreeToast(packageUp ? "上架成功" : "下架成功",context,true,0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "PACKAGEUPSHELF");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);

                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE",packageUp ? "BIND" : "UNBIND");
                map.addProperty("LOTID",lotID);
                map.addProperty("STORAGEID",storageID);
                map.addProperty("CONSUMEQTY",consumeQTY);
                map.addProperty("ISENDFLAG",isEndFlag);
                NetUtils.requestPostNet("CPC.PACKAGEUPSHELF","PACKAGEUPSHELF",context,map,callBack);
            }
        }, () -> {

        }).show(fragmentManager,"TOOLATTACHEQP");
    }

    public static void lotFTTrackIn(FragmentManager fragmentManager, Activity context, String eqpID, String lotID, List<String> carrierList, String actionType, List<String> eqpTestList, String lineID, LotInfoEntity lotInfoEntity, List<String> mPcsQtyEntity,double qty){
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                //确定

                FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                        (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context,false,fragmentManager)){
                            LotInfoEntity body = response.body().getResponse().getBody();
                            if (body.getIsSampled().equals("Y")){
                                FreeApi_DialogUtils.getConfirmDialog(context, "确定", lotID + "批次要分" + body.getNUM() + "颗样品！",
                                        () -> context.finish()).show(fragmentManager,"MES_DIALOG");
                            }else {
                                FreeApi_ToastUtils.showFreeToast("进站成功",context,true,0);
                                context.finish();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKIN");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE",actionType);
                map.addProperty("EQUIPMENTID",eqpID);
                map.addProperty("LINEID",lineID);
                map.addProperty("ISCHECKSTEPAUTHORITY",true);
                map.addProperty("LOTID",lotID);
                map.addProperty("RESERVED2",lotInfoEntity.getLOT().getUDF().getRESERVED2());
                map.addProperty("CUSTOMERCODE",lotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("CHIPNAME",lotInfoEntity.getCHIPNAME());
                map.addProperty("TUBEORRELLQTY",qty);
                map.add("DURABLELIST",new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(lotID));
                map.add("LOTLIST",new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
                map.add("SUBEQUIPMENTIDLIST",new JsonParser().parse(new Gson().toJson(eqpTestList)).getAsJsonArray());
                ArrayList<Integer> integers = new ArrayList<>();
                for (String s:mPcsQtyEntity){
                    integers.add(Integer.parseInt(s));
                }
                map.add("TUBEORRELLENDQTYLIST",new JsonParser().parse(new Gson().toJson(integers)).getAsJsonArray());
                NetUtils.requestPostNet("CPC.LOTFTTRACKIN","LOTFTTRACKIN",context,map,callBack);

            }
        }, () -> {
        }).show(fragmentManager,"SUBMIT_DIALOG");
    }

    public static void binReport(Activity context,FragmentManager fragmentManager,String actionType,String testType,String lotID,
                                 String eqpID,LotInfoEntity lotInfoEntity,List<BinInfoEntity.BinInfo> binList,String gradeNo,String eqpType,
                                 String testEqp,String StationID,String messageName,String state,Boolean isChecked){
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                //确定
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context,false,fragmentManager)){
                            FreeApi_ToastUtils.showFreeToast("提交成功",context,true,0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), messageName);
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE",actionType);
                map.addProperty("TESTTYPE",testType);
                map.addProperty("LOTID",lotID);
                map.addProperty("PRODUCTMODEL",lotInfoEntity.getPRODUCTMODEL());
                map.addProperty("WIRINGNO",lotInfoEntity.getWIRINGNO());
                map.addProperty("SEALNO",lotInfoEntity.getSEALNO());
                map.addProperty("CHIPNAME",lotInfoEntity.getCHIPNAME());
                map.addProperty("WIRESPEC",lotInfoEntity.getWIRESPEC());
                map.addProperty("LINEID",lotInfoEntity.getLINEID());
                map.addProperty("EQUIPMENTID",eqpID);
                map.addProperty("CHIPLOT",lotInfoEntity.getCHIPID());
                map.addProperty("LOTTYPE",lotInfoEntity.getLOT().getLOTTYPE());
                map.addProperty("PACKAGETYPE",lotInfoEntity.getLOT().getMAINMATTYPE());
                map.addProperty("CUSTOMERCODE",lotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("LOTCOUNT",lotInfoEntity.getLOT().getMAINQTY());
                map.addProperty("GRADENO",gradeNo);
                map.addProperty("EQPTYPE",eqpType);
                map.addProperty("TESTERIDS",testEqp);
                map.addProperty("StationID",StationID);
                map.addProperty("OBJECTRRN",lotInfoEntity.getLOT().getOBJECTRRN());
                map.addProperty("CONNECTIONSTATE",state);
                map.addProperty("TEST_RESET",isChecked);
                map.add("HBINLIST",new JsonParser().parse(new Gson().toJson(binList)).getAsJsonArray());
                NetUtils.requestPostNet(messageName,messageName,context,map,callBack);

            }
        }, () -> {
        }).show(fragmentManager,"SUBMIT_DIALOG");

    }

    public static void lotDATrackOut(Activity context, FragmentManager fragmentManager,String user,String eqpWork, String lotID,
                                       List<ScrapEntity> defectData,List<String> carrierList,LotInfoEntity lotInfoEntity,
                                       int defectNum,String trackNum,String actionType,netCallback callback){
        FreeUI_GeneralFragmentDialog freeU_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.dialog_trackout,null);
        TextView tvLotID = view.findViewById(R.id.tvLotID);
        tvLotID.setText(lotID);
        TextView tvStep = view.findViewById(R.id.tvStep);
        tvStep.setText(lotInfoEntity.getLOT().getSTEPNAME() + "/" + lotInfoEntity.getLOT().getSTEPDESC());
        TextView tvEqpID = view.findViewById(R.id.tvEqpID);
        tvEqpID.setText(eqpWork);
        TextView tvTrackInQTY = view.findViewById(R.id.tvTrackInQTY);
        tvTrackInQTY.setText(lotInfoEntity.getLOT().getMAINQTY());
        TextView tvTrackOutQTY = view.findViewById(R.id.tvTrackOutQTY);
        int mainQTY = Integer.parseInt(lotInfoEntity.getLOT().getMAINQTY());
        tvTrackOutQTY.setText(trackNum.equals("") ? String.valueOf(mainQTY - defectNum) : trackNum);
        TextView tvDefectNum = view.findViewById(R.id.tvDefectNum);
        TextView tvDefect = view.findViewById(R.id.tvDefect);
        NumberFormat numberFormat =NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        int sumDefect = 0;
        for (ScrapEntity entity : defectData){
            if (!entity.getACTIONREASON().contains("不记录良率")){
                sumDefect += Integer.parseInt(entity.getMAINQTY().equals("") ? "0" : entity.getMAINQTY());
            }
        }
        tvDefectNum.setText(String.valueOf(sumDefect));
//        String result = numberFormat.format(((float) (mainQTY - sumDefect) / (float) mainQTY) * 100);
        String result = numberFormat.format(((float) (Integer.parseInt(tvTrackOutQTY.getText().toString())) / (float) mainQTY) * 100);
        tvDefect.setText(result + "%");
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        tvCancel.setOnClickListener(view1 -> {
            freeU_generalFragmentDialog.dismiss();
//            if (callback != null){
//                callback.onFail(new RuntimeException("取消按下"));
//            }
        });
        tvConfirm.setOnClickListener(v->{
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), fragmentManager, context) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(context,false,fragmentManager)){
                        String holdstate = response.body().getResponse().getBody().getLOTLIST().get(0).getHOLDSTATE();
                        boolean flag=response.body().getResponse().getBody().isWBONDUNITS();
                        String msg="出站成功";
                        if(flag){msg="此批次低良，暂扣";}
                        FreeApi_DialogUtils.getConfirmDialog(context, "确定", holdstate.equals("On") ? "该批次良率超标！" : msg, () -> {
                            // 出站成功之后，还有回调函数，所以 需要退出主界面 自己 finish
                            callback.onSuccess(response);
                        }).show(fragmentManager,"CONFIRM_DIALOG");
                    }else {
                        callback.onFail(new RuntimeException("接口报错"));
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKOUT");
                    FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);
                    callback.onFail(new RuntimeException("接口报错"));
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE",actionType);
            map.addProperty("EQUIPMENTID",eqpWork);
            map.addProperty("ISCHECKSTEPAUTHORITY",true);
            List<LotList> lotList = new ArrayList<>();
            LotList lotList1 = new LotList(lotID);
            lotList1.setMAINQTY(String.valueOf(Integer.parseInt(trackNum.equals("")?"0":trackNum) + defectNum));
            lotList.add(lotList1);
            map.add("LOTLIST",new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
            map.add("LOTACTIONLIST",new JsonParser().parse(new Gson().toJson(defectData)).getAsJsonArray());
            map.add("DURABLELIST",new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
            NetUtils.mRequestPostNet("CPC.LOTTRACKOUT",user,"LOTTRACKOUT",context,map,callBack);
            freeU_generalFragmentDialog.dismiss();
        });

        freeU_generalFragmentDialog.setLayout(view);
        freeU_generalFragmentDialog.show(fragmentManager, "SUBMIT_DIALOG");
    }

    public static void lotWBTrackOut(Activity context, FragmentManager fragmentManager,String user,String eqpWork, String lotID,
                                     List<ScrapEntity> defectData,List<String> carrierList,LotInfoEntity lotInfoEntity,
                                     int defectNum,String trackNum,String actionType,List<String> mPcsQtyEntity,double qty,netCallback callback){
        FreeUI_GeneralFragmentDialog freeU_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.dialog_trackout,null);
        TextView tvLotID = view.findViewById(R.id.tvLotID);
        tvLotID.setText(lotID);
        TextView tvStep = view.findViewById(R.id.tvStep);
        tvStep.setText(lotInfoEntity.getLOT().getSTEPNAME() + "/" + lotInfoEntity.getLOT().getSTEPDESC());
        TextView tvEqpID = view.findViewById(R.id.tvEqpID);
        tvEqpID.setText(eqpWork);
        TextView tvTrackInQTY = view.findViewById(R.id.tvTrackInQTY);
        tvTrackInQTY.setText(lotInfoEntity.getLOT().getMAINQTY());
        TextView tvTrackOutQTY = view.findViewById(R.id.tvTrackOutQTY);
        double mainQTY = Double.parseDouble(lotInfoEntity.getLOT().getMAINQTY());
        tvTrackOutQTY.setText(trackNum.equals("") ? String.valueOf(mainQTY - defectNum) : trackNum);
        TextView tvDefectNum = view.findViewById(R.id.tvDefectNum);
        TextView tvDefect = view.findViewById(R.id.tvDefect);
        NumberFormat numberFormat =NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        int sumDefect = 0;
        for (ScrapEntity entity : defectData){
            if (!entity.getACTIONREASON().contains("不记录良率")){
                sumDefect += Integer.parseInt(entity.getMAINQTY().equals("") ? "0" : entity.getMAINQTY());
            }
        }
        tvDefectNum.setText(String.valueOf(sumDefect));
//        String result = numberFormat.format(((float) (mainQTY - sumDefect) / (float) mainQTY) * 100);
        String result = numberFormat.format(((float) (Double.parseDouble(tvTrackOutQTY.getText().toString())) / (float) mainQTY) * 100);
        tvDefect.setText(result + "%");
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        tvCancel.setOnClickListener(view1 -> {
            freeU_generalFragmentDialog.dismiss();
//            if (callback != null){
//                callback.onFail(new RuntimeException("取消按下"));
//            }
        });
        tvConfirm.setOnClickListener(v->{
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), fragmentManager, context) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(context,false,fragmentManager)){
                        String holdstate = response.body().getResponse().getBody().getLOTLIST().get(0).getHOLDSTATE();
                        boolean flag=response.body().getResponse().getBody().isWBONDUNITS();
                        if (!response.body().getResponse().getBody().getHOLDMSG().equals("")) {
                            FreeUI_GeneralFragmentDialog tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", response.body().getResponse().getBody().getHOLDMSG(), () -> {
                                callback.onSuccess(response);
                            });
                            tipDialog.show(fragmentManager, "DIALOG");
                            return;
                        }
                        String msg="出站成功";
                        if (!response.body().getResponse().getBody().getSHOWMSG().equals("")){
                            msg = msg+","+response.body().getResponse().getBody().getSHOWMSG();
                        }
                        if(flag){msg="此批次低良，暂扣";}
                        FreeApi_DialogUtils.getConfirmDialog(context, "确定", holdstate.equals("On") ? "该批次良率超标！" : msg, () -> {
                            // 出站成功之后，还有回调函数，所以 需要退出主界面 自己 finish
                            callback.onSuccess(response);
                        }).show(fragmentManager,"CONFIRM_DIALOG");
                    }else {
                        callback.onFail(new RuntimeException("接口报错"));
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKOUT");
                    FreeApi_ToastUtils.showFreeToast("连接服务器失败",context,false,0);
                    callback.onFail(new RuntimeException("接口报错"));
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE",actionType);
            map.addProperty("EQUIPMENTID",eqpWork);
            map.addProperty("ISCHECKSTEPAUTHORITY",true);
            List<LotList> lotList = new ArrayList<>();
            LotList lotList1 = new LotList(lotID);
            lotList1.setMAINQTY(String.valueOf(Integer.parseInt(trackNum.equals("")?"0":trackNum) + defectNum));
            lotList.add(lotList1);
            map.add("LOTLIST",new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
            map.add("LOTACTIONLIST",new JsonParser().parse(new Gson().toJson(defectData)).getAsJsonArray());
            map.add("DURABLELIST",new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
            ArrayList<Integer> sList = new ArrayList<>();
            for (String s : mPcsQtyEntity) {
                sList.add(Integer.parseInt(s));
            }
            map.addProperty("TUBEORRELLQTY",qty);
            map.add("TUBEORRELLENDQTYLIST",new JsonParser().parse(new Gson().toJson(sList)).getAsJsonArray());
            NetUtils.mRequestPostNet("CPC.LOTTRACKOUT",user,"LOTTRACKOUT",context,map,callBack);
            freeU_generalFragmentDialog.dismiss();
        });

        freeU_generalFragmentDialog.setLayout(view);
        freeU_generalFragmentDialog.show(fragmentManager, "SUBMIT_DIALOG");
    }

}
